/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.awt;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

/*
 * CanvasStack extends the (unrelated to HTML Canvas) java.awt.Canvas class
 * and draw the stack of Command added to it. 
 */
@SuppressWarnings("serial")
public class CanvasStack extends Canvas
{
	private LinkedList<Command> m_commands = new LinkedList<Command>();

	public void addCommand(Command c)
	{
		synchronized(m_commands)
		{
			m_commands.add(c);
		}
	}

	public void clear()
	{
		synchronized(m_commands)
		{
			m_commands.clear();
		}
	}

	@Override
	synchronized public void paint(Graphics g1) 
	{
		super.paint(g1);

		Graphics2D g = (Graphics2D)g1;
		LinkedList<CanvasState> statestack = new LinkedList<CanvasState>();
		CanvasState state = new CanvasState();
		statestack.add(state);

		Map<Object, Object> rendering = new HashMap<Object,Object>();
		rendering.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.addRenderingHints(rendering);

		synchronized(m_commands)
		{
			for (Iterator<Command> i=m_commands.iterator(); i.hasNext(); )
			{
				Command c = i.next();
				c.applyCommand(statestack, g, this);
			}
		}
	}



}
